﻿<%@ Page Title="" Language="C#" MasterPageFile="~/TsSite.Master" AutoEventWireup="true" CodeBehind="ServiceList.aspx.cs" Inherits="TireService.Web.View.OperatorView.ServiceList" %>

<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a"
    Namespace="DevExpress.Web.ASPxHiddenField" TagPrefix="dx" %>

<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxLoadingPanel" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxCallback" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxCallbackPanel" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxGridView.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxGridView" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.ASPxEditors.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxEditors" TagPrefix="dx" %>
<%@ Register Assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" Namespace="DevExpress.Web.ASPxPopupControl" TagPrefix="dx" %>
<%@ Register assembly="DevExpress.Web.v11.1, Version=11.1.12.0, Culture=neutral, PublicKeyToken=b88d1754d700e49a" namespace="DevExpress.Web.ASPxPanel" tagprefix="dx" %>


<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

    <!-- Javascript variables used in control -->
    <script language="javascript" type="text/javascript">

        function ShowMarkServiceAsCompleted(serviceid) {
            // Store selected service in hidden field
            hfServiceCarScheduleDetailId.Set("ServiceCarScheduleDetailId", serviceid);

            // Show modal popup for mark service complete selection
            pcMarkServiceAsCompleted.Show();
        }

        // Perform callback that marks selected services as completed depending on choise email is send to customer
        function MarkServiceAsCompleted(completiontype) {
            var answer = confirm("Færdigmeld sag?")
            if (answer) {
                // Prepare input (0: ServiceCarScheduleDetailId, 1: CompletionType (0: Send email+Trustpilot, 1: Send email or 2: Only close, no email)
                var input = hfServiceCarScheduleDetailId.Get("ServiceCarScheduleDetailId") + '|' + completiontype;
                
                // Hide modal popup
                pcMarkServiceAsCompleted.Hide();

                // Show loading panel
                LoadingPanel.Show();

                // Run callback function to mark service as completed
                callMarkServiceCompleted.PerformCallback(input);
            }
        }

        // Function that runs when modal popup for marking service as completed closes
        function MarkServiceAsCompletedModalPopupClosed() {
            // Reset label with service id stored
            hfServiceCarScheduleDetailId.Set("ServiceCarScheduleDetailId", "");
        }

        // Function that runs when server side completed processing of handling markservice as completed
        function OnCallbackMarkServiceCompleted(s, e) {
            BindDayPlan();

            LoadingPanel.Hide();
        }

        // Function that will show popup for selection other actions for servicedrive
        function Action(serviceid) {
            // Store selected service in hidden field
            hfServiceCarScheduleDetailId.Set("ServiceCarScheduleDetailId", serviceid);

            // Show modal popup for other actions
            pcServiceListAction.Show();
            // TODO: Implement code
            //alert('Ikke implementeret endnu');
            //pcServiceListAction.Show();
        }

        function OtherAction(action) {
            var answer = confirm("Udfør handling?")
            if (answer) {
                // Prepare input (0: ServiceCarScheduleDetailId, 1: Action (0: Replan service, 1: complete service - customer kept tires)
                var input = hfServiceCarScheduleDetailId.Get("ServiceCarScheduleDetailId") + '|' + action;

                // Hide modal popup
                pcServiceListAction.Hide();

                // Show loading panel
                LoadingPanel.Show();

                // Run callback function for other action
                callOtherAction.PerformCallback(input);
            }
        }

        // Function that runs when server side completed processing other action
        function OnCallbackOtherActionCompleted(s, e) {
            BindDayPlan();

            LoadingPanel.Hide();
        }

        function SendSms() {
            alert('Ikke implementeret endnu');
        }

        // Function that runs when server side completed processing of getting previous/next scheduled date
        function OnCallbackGetScheduleDateCompleted(s, e) {

            if (e.result != null & e.result != '') {
                lblScheduleDate.SetText(e.result);
                BindDayPlan();
            } 
            else {
                alert('Intet kørselsskema blev fundet');
            }

            LoadingPanel.Hide();
        }

        function BindDayPlan() {
            var input = lblServiceCarId.GetText() + '|' + lblScheduleDate.GetText();
            gvDayPlan.PerformCallback(input);
        }

        // Function that return error in case callback to server failed
        function CallBackError(s, e) {
            LoadingPanel.Hide();

            alert('Fejl opstod: ' + e.message);
        }

        function GetPreviousDayPlan() {
            // Prepare input values (0: next(1) or previous(-1), 1: servicecarid, 2: current scheduledate)
            var input = '-1|' + lblServiceCarId.GetText() + '|' + lblScheduleDate.GetText();
            callbackGetScheduleDate.PerformCallback(input);

            //alert('Ikke implementeret endnu');
        }

        function GetNextDayPlan() {
            // Prepare input values (0: next(1) or previous(-1), 1: servicecarid, 2: current scheduledate)
            var input = '1|' + lblServiceCarId.GetText() + '|' + lblScheduleDate.GetText();
            callbackGetScheduleDate.PerformCallback(input);

            //alert('Ikke implementeret endnu');
        }

        function PrintDayPlan(s, e) {
            // Set focus on first row
            gvDayPlan.SetFocusedRowIndex(0);

            // Get first row values and trigger event ShowPlanningSchedule
            gvDayPlan.GetRowValues(gvDayPlan.GetFocusedRowIndex(), 'ServiceCarId;ScheduleDate', ShowPlanningSchedule);
        }

        // Show planning schedule in pdf
        function ShowPlanningSchedule(values) {
            if (values == null) return;
            if (values.length == 0) return;

            var scheduleDate = new Date(values[1]);
            var stringDate = scheduleDate.getDate() + "-" + (1 + scheduleDate.getMonth()) + "-" + scheduleDate.getFullYear();

            window.open('../../Reports/View/ServiceCarScheduleReportView.aspx?ServiceCarId=' + values[0] + "&ScheduleDate=" + stringDate, 'Kørselsskema');

            // Deselect all lines
            gvDayPlan.SelectAllRowsOnPage(false);
        }

    </script>
    <dx:ASPxLoadingPanel ID="LoadingPanel" Text="Vent venligst..." ClientInstanceName="LoadingPanel" runat="server" Modal="True">
    </dx:ASPxLoadingPanel>
    
    <!-- Hidden value to store selected servicecarscheduledetailid in -->
    <dx:ASPxHiddenField ID="hfServiceCarScheduleDetailId" ClientInstanceName="hfServiceCarScheduleDetailId" runat="server">
    </dx:ASPxHiddenField>


    <!-- Modal popup that fires when user select mark service as completed -->
    <dx:ASPxPopupControl ID="pcMarkServiceAsCompleted" runat="server" CloseAction="CloseButton"
        Modal="True" PopupHorizontalAlign="WindowCenter" PopupVerticalAlign="WindowCenter"
        ClientInstanceName="pcMarkServiceAsCompleted" HeaderText="Færdigmeld sag" EnableAnimation="False"
        EnableViewState="False" ClientIDMode="AutoID" EnableHotTrack="False">
        <ClientSideEvents Closing="function(s, e) { MarkServiceAsCompletedModalPopupClosed(); }" />
        <LoadingPanelStyle ImageSpacing="5px">
        </LoadingPanelStyle>
        <ContentCollection>
            <dx:PopupControlContentControl ID="PopupControlContentControl2" runat="server">
                <dx:ASPxPanel ID="Panel1" runat="server" DefaultButton="btOK">
                    <PanelCollection>
                        <dx:PanelContent ID="PanelContent1" runat="server">
                            <table width="350px">
                                <tr>
                                    <td colspan="3">
                                        <h1>Færdigmelding af sag</h1>
                                        <p class="normaltext">
                                            Vælg metode
                                        </p>
                                        
                                    </td>
                                </tr>
                                <tr>
                                    <td valign="top">
                                        <dx:ASPxButton ID="btnMarkCompletedPlusReview" runat="server" Text="Færdigmeld + Trustpilot link" AutoPostBack="false" UseSubmitBehavior="False" Width="175px" Height="40px">
                                            <ClientSideEvents Click="function() { MarkServiceAsCompleted(0); }" />
                                        </dx:ASPxButton>
                                    </td>
                                    <td valign="top">
                                        <dx:ASPxButton ID="btnMarkCompletedWithMail" runat="server" Text="Færdigmeld + Mail bekræftelse" AutoPostBack="false" UseSubmitBehavior="False" Width="175px" Height="40px">
                                            <ClientSideEvents Click="function() { MarkServiceAsCompleted(1); }" />
                                        </dx:ASPxButton>

                                    </td>
                                    <td valign="top">
                                        <dx:ASPxButton ID="btnMarkCompletedWithoutMail" runat="server" Text="Færdigmeld uden mail" AutoPostBack="false" UseSubmitBehavior="False" Width="175px" Height="40px">
                                            <ClientSideEvents Click="function() { MarkServiceAsCompleted(2); }" />
                                        </dx:ASPxButton>

                                    </td>
                                </tr>
                            </table>
                        </dx:PanelContent>
                    </PanelCollection>
                </dx:ASPxPanel>
            </dx:PopupControlContentControl>
        </ContentCollection>
    </dx:ASPxPopupControl>

    <!-- Modal popup that fires when user select other actions -->
    <dx:ASPxPopupControl ID="pcServiceListAction" runat="server" CloseAction="CloseButton"
        Modal="True" PopupHorizontalAlign="WindowCenter" PopupVerticalAlign="WindowCenter"
        ClientInstanceName="pcServiceListAction" HeaderText="Færdigmeld sag" EnableAnimation="False"
        EnableViewState="False" ClientIDMode="AutoID" EnableHotTrack="False">
        <LoadingPanelStyle ImageSpacing="5px">
        </LoadingPanelStyle>
        <ContentCollection>
            <dx:PopupControlContentControl ID="PopupControlContentControl3" runat="server">
                <dx:ASPxPanel ID="ASPxPanel1" runat="server" DefaultButton="btOK">
                    <PanelCollection>
                        <dx:PanelContent ID="PanelContent2" runat="server">
                            <table width="350px">
                                <tr>
                                    <td colspan="3">
                                        <h1>Handlinger</h1>
                                        <p class="normaltext">
                                            Vælg en handling
                                        </p>
                                    </td>
                                </tr>
                                <tr>
                                    <td valign="top">
                                        <dx:ASPxButton ID="btnReplan" runat="server" Text="Sag ikke taget - genplanlæg" AutoPostBack="false" UseSubmitBehavior="False" Width="175px" Height="40px">
                                            <ClientSideEvents Click="function() { OtherAction(0); }" />
                                        </dx:ASPxButton>
                                    </td>
                                    <td valign="top">
                                        <dx:ASPxButton ID="btnCustomerKeeptTires" runat="server" Text="Færdigmeld + Kunde beholdt hjul" AutoPostBack="false" UseSubmitBehavior="False" Width="175px" Height="40px">
                                            <ClientSideEvents Click="function() { OtherAction(1); }" />
                                        </dx:ASPxButton>

                                    </td>
                                    <td valign="top">
                                        <dx:ASPxButton ID="btnSendSms" runat="server" Text="Send sms" AutoPostBack="false" UseSubmitBehavior="False" Width="175px" Height="40px">
                                            <ClientSideEvents Click="function() { SendSms(); }" />
                                        </dx:ASPxButton>

                                    </td>
                                </tr>
                            </table>
                        </dx:PanelContent>
                    </PanelCollection>
                </dx:ASPxPanel>
            </dx:PopupControlContentControl>
        </ContentCollection>
    </dx:ASPxPopupControl>


    <table cellpadding="0" cellspacing="2" border="0">
        <tr>
            <td>
                <img id="imgOperatorView" alt="" src="~/Images/Medium/wrench.png" runat="server" style="width: 32px; height: 32px" />
            </td>
            <td valign="middle">
                <h2><asp:Label ID="lblOperationView" runat="server" Text="Kørselsskema"></asp:Label></h2>
            </td>
        </tr>
    </table>
    
    <dx:ASPxCallback ID="callMarkServiceCompleted" ClientInstanceName="callMarkServiceCompleted" runat="server" OnCallback="callMarkServiceCompleted_Callback">
        <ClientSideEvents CallbackError="CallBackError" CallbackComplete="OnCallbackMarkServiceCompleted" />
    </dx:ASPxCallback>

    <dx:ASPxCallback ID="callOtherAction" ClientInstanceName="callOtherAction" runat="server" OnCallback="callOtherAction_Callback">
        <ClientSideEvents CallbackError="CallBackError" CallbackComplete="OnCallbackOtherActionCompleted" />
    </dx:ASPxCallback>

    <dx:ASPxCallback ID="callbackGetScheduleDate" ClientInstanceName="callbackGetScheduleDate" runat="server" OnCallback="callbackGetScheduleDate_Callback">
        <ClientSideEvents CallbackError="CallBackError" CallbackComplete="OnCallbackGetScheduleDateCompleted" />
    </dx:ASPxCallback>

    <table>
        <tr>
            <td>
                <dx:ASPxLabel ID="lblCar" ClientInstanceName="lblCar" runat="server" Text="Service bil" Font-Bold="true" Font-Size="Large">
                </dx:ASPxLabel>
            </td>
            <td>
                <dx:ASPxLabel ID="lblServiceCarId" ClientInstanceName="lblServiceCarId" runat="server" Text="" Font-Size="Large">
                </dx:ASPxLabel>
            </td>
        </tr>
        <tr>
            <td>
                <dx:ASPxLabel ID="lblDate" ClientInstanceName="lblDate" runat="server" Text="Dato" Font-Bold="true" Font-Size="Large">
                </dx:ASPxLabel>
            </td>
            <td>
                <dx:ASPxLabel ID="lblScheduleDate" ClientInstanceName="lblScheduleDate" runat="server" Text="" Font-Size="Large">
                </dx:ASPxLabel>
            </td>
        </tr>
    </table>
    <table>
        <tr>
            <td>
                <dx:ASPxButton ID="btnPrevious" ClientInstanceName="btnPrevious" runat="server" Text="Tilbage" Font-Size="Large" Width="300px" Height="40px" AutoPostBack="False" UseSubmitBehavior="False">
                    <ClientSideEvents Click="function(s, e) { GetPreviousDayPlan(); }" />
                </dx:ASPxButton>
            </td>
            <td>
                <dx:ASPxButton ID="btnNext" ClientInstanceName="btnNext" runat="server" Text="Frem" Font-Size="Large" Width="300px" Height="40px" AutoPostBack="False" UseSubmitBehavior="False">
                    <ClientSideEvents Click="function(s, e) { GetNextDayPlan(); }" />
                </dx:ASPxButton>
            </td>
            <td>
                <dx:ASPxButton ID="btnPrintDayPlan" ClientInstanceName="btnPrintDayPlan" runat="server" Text="Print skema" Font-Size="Large" Width="300px" Height="40px" AutoPostBack="False" UseSubmitBehavior="False">
                    <ClientSideEvents Click="function(s, e) { PrintDayPlan(); }" />
                </dx:ASPxButton>
            </td>
        </tr>
    </table>
    <table width="100%">
        <tr>
            <td valign="top" colspan="3">
                <dx:ASPxGridView ID="gvDayPlan" runat="server" Caption="Kørselsskema" Width="100%"
                    KeyFieldName="ServiceCarScheduleDetailId" AutoGenerateColumns="False" ClientInstanceName="gvDayPlan"
                    OnCustomCallback="gvDayPlan_CustomCallback" OnHtmlRowPrepared="gvDayPlan_HtmlRowPrepared" OnCustomUnboundColumnData="gvDayPlan_CustomUnboundColumnData">
                    <SettingsBehavior ProcessFocusedRowChangedOnServer="false" AllowFocusedRow="true" />
                    <SettingsPager AlwaysShowPager="True" PageSize="25" Position="Bottom">
                        <Summary 
                            AllPagesText="Sider: {0} - {1} ({2} sager)" 
                            Text="Side {0} af {1} ({2} sager)" />
                    </SettingsPager>
                    <Columns>
                        <dx:GridViewDataTextColumn FieldName="CustomerId" VisibleIndex="0" Caption="Kundenr" Visible="false" />
                        <dx:GridViewDataTextColumn FieldName="ServiceCarScheduleDetailId" Visible="false" VisibleIndex="0" />
                        <dx:GridViewDataTextColumn FieldName="ScheduleDate" Visible="false" VisibleIndex="0" />
                        <dx:GridViewDataTextColumn FieldName="ServiceCarId" Visible="false" VisibleIndex="0" />
                        <dx:GridViewDataTextColumn FieldName="ToLongitude" Visible="false" VisibleIndex="0" />
                        <dx:GridViewDataTextColumn FieldName="ToLatitude" Visible="false" VisibleIndex="0" />
                        <dx:GridViewDataTextColumn FieldName="TaskId" Visible="false" VisibleIndex="0" />
                        <dx:GridViewDataTextColumn FieldName="CarId" Visible="false" VisibleIndex="0" />
                        <dx:GridViewDataTextColumn FieldName="ContainerId" Visible="false" VisibleIndex="0" />
                        <dx:GridViewDataTextColumn FieldName="ScheduleDetailTypeId" Visible="false" VisibleIndex="0" />
                        <dx:GridViewDataTextColumn FieldName="Note" Visible="false" VisibleIndex="0" />
                        <dx:GridViewDataTextColumn Caption="Start" FieldName="StartTime" VisibleIndex="0" />
                        <dx:GridViewDataTextColumn Caption="Slut" FieldName="EndTime" VisibleIndex="1" />
                        <dx:GridViewDataTextColumn Caption="Navn" FieldName="Name" VisibleIndex="2" />
                        <dx:GridViewDataTextColumn Caption="Adresse" FieldName="ToAddress" VisibleIndex="3" />
                        <dx:GridViewDataTextColumn Caption="Postnr" FieldName="ToZipCode" VisibleIndex="4" />
                        <dx:GridViewDataTextColumn Caption="By" FieldName="ToCity" VisibleIndex="5" />
                        <dx:GridViewDataTextColumn Caption="Telefon1" FieldName="Phone1" VisibleIndex="6" />
                        <dx:GridViewDataTextColumn Caption="Reg.nr." FieldName="RegistrationNumber" VisibleIndex="7" />
                        <dx:GridViewDataTextColumn FieldName="CarBrandDescription" VisibleIndex="7" Visible="false" />
                        <dx:GridViewDataTextColumn FieldName="Model" VisibleIndex="7" Visible="false" />
                        <dx:GridViewDataTextColumn FieldName="Color" VisibleIndex="7" Visible="false" />
                        <dx:GridViewDataTextColumn Caption="Bil" UnboundType="String" FieldName="CarDetailDescription" VisibleIndex="7" />
                        <dx:GridViewDataTextColumn Caption="Lager" FieldName="ContainerNumber" VisibleIndex="7" Visible="false" />
                        <dx:GridViewDataTextColumn Caption="Lagerplads" FieldName="CargoRoomNumber" VisibleIndex="8" />
                        <dx:GridViewDataTextColumn Caption="Opgaver" UnboundType="String" FieldName="ServiceTaskDetails" VisibleIndex="8" />
                        <dx:GridViewDataTextColumn FieldName="CustomerConfirmed" VisibleIndex="8" Visible="false" />
                        <dx:GridViewDataCheckColumn FieldName="WorkCompleted" VisibleIndex="8" Caption="Færdigmeldt" Visible="false" />
                        <dx:GridViewDataColumn>
                            <DataItemTemplate>
                                <dx:ASPxButton ID="btnOtherAction" runat="server" ClientInstanceName="btnOtherAction" Text="Anden handling" ToolTip="Klik for at udfører anden handling" AutoPostBack="false"
                                    ClientSideEvents-Click='<%# "function(s, e) { Action(" + Container.KeyValue + "); }" %>'>
                                </dx:ASPxButton>                                           
                            </DataItemTemplate>
                        </dx:GridViewDataColumn>
                        <dx:GridViewDataColumn>
                            <DataItemTemplate>
                                <dx:ASPxButton ID="btnInsert" runat="server" ClientInstanceName="btnInsert" Text="Færdigmeld sag" ToolTip="Klik for at færdigmelde sag" AutoPostBack="false"
                                    ClientSideEvents-Click='<%# "function(s, e) { ShowMarkServiceAsCompleted(" + Container.KeyValue + "); }" %>'>
                                </dx:ASPxButton>                                           
                            </DataItemTemplate>
                        </dx:GridViewDataColumn>
                    </Columns>
                    <SettingsLoadingPanel Mode="ShowOnStatusBar" />
                    <Styles>
                        <Header ImageSpacing="5px" SortingImageSpacing="5px" />
                    </Styles>
                </dx:ASPxGridView>
            </td>
        </tr>
    </table>

</asp:Content>
